using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;

/// <summary>
/// Summary description for PolicyBll
/// </summary>
public class PolicyBll
{
    DBAccess db = new DBAccess();

    public PolicyBll()
    {

    }
        
    public DataTable getAllPolicy()
    {
        return db.executeSelect("GetAllPolicy");
    }

    public DataTable getPolicyIsPaid()
    {
        return db.executeSelect("GetPolicyIsPaid");
    }

    public DataTable getPolicyIsNotPaid()
    {
        return db.executeSelect("GetPolicyNotPaid");
    }

    public Policy getPolicyById(int policyId)
    {
        Policy policy = new Policy();
        DataTable dt = new DataTable();
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@PolicyId", SqlDbType.Int);
        para[0].Value = policyId;
        dt = db.executeSelect("GetPolicyById", para);
        policy.PolicyId = int.Parse(dt.Rows[0][0].ToString());
        policy.UserId = int.Parse(dt.Rows[0][1].ToString());
        policy.InsuranceType = dt.Rows[0][2].ToString();
        policy.PolicyValue = int.Parse(dt.Rows[0][3].ToString());
        policy.Period = dt.Rows[0][4].ToString();
        policy.Premium = int.Parse(dt.Rows[0][5].ToString());
        policy.ContractDate = DateTime.Parse(dt.Rows[0][6].ToString());        
        return policy;
    }

    public Policy getPolicyByUserId(int userId)
    {
        Policy policy = new Policy();
        DataTable dt = new DataTable();
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@UserId", SqlDbType.Int);
        para[0].Value = userId;
        dt = db.executeSelect("GetPolicyByUserId", para);
        policy.PolicyId = int.Parse(dt.Rows[0][0].ToString());
        policy.UserId = int.Parse(dt.Rows[0][1].ToString());
        policy.InsuranceType = dt.Rows[0][2].ToString();
        policy.PolicyValue = int.Parse(dt.Rows[0][3].ToString());
        policy.Period = dt.Rows[0][4].ToString();
        policy.Premium = float.Parse(dt.Rows[0][5].ToString());
        policy.ContractDate = DateTime.Parse(dt.Rows[0][6].ToString());
        return policy;
    }

    public bool insertPolicy(Policy policy)
    {
        bool result;
        SqlParameter[] para = new SqlParameter[7];
        para[0] = new SqlParameter("@UserId", SqlDbType.Int);
        para[0].Value = policy.UserId;
        para[1] = new SqlParameter("@InsuranceType", SqlDbType.NVarChar);
        para[1].Value = policy.InsuranceType;
        para[2] = new SqlParameter("@PolicyValue", SqlDbType.Int);
        para[2].Value = policy.PolicyValue;
        para[3] = new SqlParameter("@Period", SqlDbType.NVarChar);
        para[3].Value = policy.Period;
        para[4] = new SqlParameter("@Premium", SqlDbType.Float);
        para[4].Value = policy.Premium;
        para[5] = new SqlParameter("@ContractDate", SqlDbType.DateTime);
        para[5].Value = policy.ContractDate;
        para[6] = new SqlParameter("@Paid", SqlDbType.Bit);
        para[6].Value = policy.Paid; 
        if (db.execute("InsertPolicy", para) > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }

    public bool updatePolicyPaidById(Policy policy)
    {
        bool result;
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@PolicyId", SqlDbType.Int);
        para[0].Value = policy.PolicyId;
        if (db.execute("UpdatePolicyPaidById", para) > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }

    public bool deletePolicyById(int PolicyId)
    {
        bool result;
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@PolicyId", SqlDbType.Int);
        para[0].Value = PolicyId;
        if (db.execute("DeletePolicyById", para) > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }

    public bool deletePolicyByUserId(int userId)
    {
        bool result;
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@UserId", SqlDbType.Int);
        para[0].Value = userId;
        if (db.execute("DeletePolicyByUserId", para) > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }

}
